<?php
class classify extends Zcontroller{
	
	function __construct() {
		parent::__construct();
		//menu current page
		Zsession::setDataToSession("current_page", "classify");	
		//$this->load_translation(array("Classified"));	
		
		
	}
	
	function index(){
    	$this->load->model("job_announcement_model");
        $this->load->model("m_classify");
        $this->load->model("m_property");
        $this->load->model("m_jobwanted");
		$this->load->model("model_vote_management");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    
    	$data['view_all_ads']=$this->m_classify->get_view_all_ads();
    	
    	$Z_TRANS = Translation::getInstance();
    	
    	$data =array();
    	$config=array();
    	$config['base_url'] = base_url().'index.php/classify/page/';
    	$total_ads=$this->m_classify->count_ads();
      	$config['total_rows'] = count($total_ads->result());
      	$config['per_page'] = '26';
      	$config['num_links'] = '9';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
    	$data['all_ads']=$this->m_classify->select_all_product_ads($config['per_page'],$this->uri->segment(3));
    	$data['all_cate']=$this->m_classify->get_all_classifycategory();
            
    	$this->load->template("templates/general","classify_ads/classify_view_all", $Z_TRANS->value('menu_classified_ads'),$data);
    }
    
	function page(){
    	$this->load->model("m_classify");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$config['base_url'] = base_url().'index.php/classify/page/';
    	$total_ads=$this->m_classify->count_ads();
      	$config['total_rows'] = count($total_ads->result());
      	$config['per_page'] = '26';
      	$config['num_links'] = '9';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
    	$data['all_ads']=$this->m_classify->select_all_product_ads($config['per_page'],$this->uri->segment(3));
    	$data['all_cate']=$this->m_classify->get_all_classifycategory();
    	
    	    $data['latest_ads']=$this->m_classify->get_product_latest();
            $data['view_all_ads']=$this->m_classify->get_view_all_ads();
    	    $data['cat_car']=$this->m_classify->get_view_by_cat("1");
            $data['cat_phone']=$this->m_classify->get_view_by_cat("3");
            $data['cat_phone_acc']=$this->m_classify->get_view_by_cat("19");
            $data['cat_computer']=$this->m_classify->get_view_by_cat("2");
            $data['cat_computer_acc']=$this->m_classify->get_view_by_cat("7");
            $data['cat_books']=$this->m_classify->get_view_by_cat("15");
            $data['cat_camera']=$this->m_classify->get_view_by_cat("8");
            
            $data['cat_cloth']=$this->m_classify->get_view_by_cat("12");
            $data['cat_dvd']=$this->m_classify->get_view_by_cat("14");
            $data['cat_flower']=$this->m_classify->get_view_by_cat("13");
            $data['cat_Health']=$this->m_classify->get_view_by_cat("16");
            
            $data['cat_Home_app']=$this->m_classify->get_view_by_cat("10");
            $data['cat_Health']=$this->m_classify->get_view_by_cat("16");
            $data['cat_property']=$this->m_classify->get_view_properties();
            $data['cat_jewellery']=$this->m_classify->get_view_by_cat("9");
            
            $data['cat_motorbike']=$this->m_classify->get_view_by_cat("18");
            $data['cat_jewellery']=$this->m_classify->get_view_by_cat("9");
            $data['cat_other']=$this->m_classify->get_view_by_cat("11");
            $data['cat_job_wanted']=$this->m_classify->get_view_job_wanted();
            $data['cat_job_ann']=$this->m_classify->get_view_job_ann();
            
    	$this->load->template("templates/general","classify_ads/classify_view_all", "Classify Ads",$data);
    }
    
    function goto_classify_computer(){
    	$this->load->model("m_classify");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$productdes="Computer";
    	$config['base_url'] = base_url().'index.php/classify/goto_classify_product/';
    	$total_ads=$this->m_classify->count_classify_product($productdes);
      	$config['total_rows'] = count($total_ads->result());
      	$config['per_page'] = '26';
      	$config['num_links'] = '9';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
    	$data['all_ads']=$this->m_classify->get_classify_product($productdes,$config['per_page'],$this->uri->segment(3));
    	$data['all_cate']=$this->m_classify->get_all_classifycategory();
    	$this->load->template("templates/general","classify_ads/classify_view_all", "Classify Ads",$data);
    }
    
	function goto_classify_mobile(){
    	$this->load->model("m_classify");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$productdes="Mobile PDAs";
    	$config['base_url'] = base_url().'index.php/classify/goto_classify_mobile/';
    	$total_ads=$this->m_classify->count_classify_product($productdes);
      	$config['total_rows'] = count($total_ads->result());
      	$config['per_page'] = '26';
      	$config['num_links'] = '9';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
      	$data['all_cate']=$this->m_classify->get_all_classifycategory();
    	$data['all_ads']=$this->m_classify->get_classify_product($productdes,$config['per_page'],$this->uri->segment(3));
    	$this->load->template("templates/general","classify_ads/classify_view_all", "Classify Ads",$data);
    }
	function goto_classify_home(){
    	$this->load->model("m_classify");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$productdes="Home Appliances";
    	$config['base_url'] = base_url().'index.php/classify/goto_classify_home/';
    	$total_ads=$this->m_classify->count_classify_product($productdes);
      	$config['total_rows'] = count($total_ads->result());
      	$config['per_page'] = '26';
      	$config['num_links'] = '9';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
      	$data['all_cate']=$this->m_classify->get_all_classifycategory();
    	$data['all_ads']=$this->m_classify->get_classify_product($productdes,$config['per_page'],$this->uri->segment(3));
    	$this->load->template("templates/general","classify_ads/classify_view_all", "Classify Ads",$data);
    }
    function dp($product_id){
    	$this->load->model("m_classify");
    	$this->load->model("m_comment");
    	$Z_TRANS = Translation::getInstance();
    	$data=array();
    	$this->m_classify->addnew_product_hit($product_id);
    	$data['all_cate']=$this->m_classify->get_all_classifycategory();
    	$data['ads_detail']=$this->m_classify->get_product_detail($product_id);
    	
    	$data['comment']=$this->m_comment->get_classify_comment_pro($product_id);
    	
    	   $data['latest_ads']=$this->m_classify->get_product_latest();
            $data['view_all_ads']=$this->m_classify->get_view_all_ads();
    	    $data['cat_car']=$this->m_classify->get_view_by_cat("1");
            $data['cat_phone']=$this->m_classify->get_view_by_cat("3");
            $data['cat_phone_acc']=$this->m_classify->get_view_by_cat("19");
            $data['cat_computer']=$this->m_classify->get_view_by_cat("2");
            $data['cat_computer_acc']=$this->m_classify->get_view_by_cat("7");
            $data['cat_books']=$this->m_classify->get_view_by_cat("15");
            $data['cat_camera']=$this->m_classify->get_view_by_cat("8");
            
            $data['cat_cloth']=$this->m_classify->get_view_by_cat("12");
            $data['cat_dvd']=$this->m_classify->get_view_by_cat("14");
            $data['cat_flower']=$this->m_classify->get_view_by_cat("13");
            $data['cat_Health']=$this->m_classify->get_view_by_cat("16");
            
            $data['cat_Home_app']=$this->m_classify->get_view_by_cat("10");
            $data['cat_Health']=$this->m_classify->get_view_by_cat("16");
            $data['cat_property']=$this->m_classify->get_view_properties();
            $data['cat_jewellery']=$this->m_classify->get_view_by_cat("9");
            
            $data['cat_motorbike']=$this->m_classify->get_view_by_cat("18");
            $data['cat_jewellery']=$this->m_classify->get_view_by_cat("9");
            $data['cat_other']=$this->m_classify->get_view_by_cat("11");
            $data['cat_job_wanted']=$this->m_classify->get_view_job_wanted();
            $data['cat_job_ann']=$this->m_classify->get_view_job_ann();
    	$this->load->template("templates/general","classify_ads/classify_view_detail", $Z_TRANS->value('menu_classified_ads')." ".$Z_TRANS->value('title_detail'),$data);
    }
    function goto_addnew_classify(){
    	$this->load->model("m_classify");
    	$this->load->model("m_users");
    	$Z_USER = Zsession::getUserFromSession(); 
    	$userid=$Z_USER["user_id"];
    	if(isset($userid)){
    		 $data=array();
    	$data['cbocategory']='';
	    	$data['txtproductname']='';
	    	$data['txtdesc']='';
	    	$data['txtprice']='';
	    	$data['txtcontactname']='';
	    	$data['txtaddress']='';
	    	$data['txtphone']='';
	    	$data['txtfax']='';
	    	$data['txtemail']='';
	    	$data["error_message"] = '';
	    	$data['all_cate']=$this->m_classify->get_all_classifycategory();
	    	$data["success_message"] = Zsession::getFlash("success_message");
	    	$this->load->template("templates/general","classify_ads/addnew_classify", "Classify Ads",$data);
    	}else{
    		redirect(site_url("users"));
    	}
    }
	/*function findexts($filename) {
		if (!empty($filename)) {
			$filename = strtolower($filename);
			$extArray = explode(".", $filename);
			$exts="";
			foreach ($extArray as $exten):
				$exts=$exten;
			endforeach;
			return $exts;
				//echo "<pre>"; var_dump($exts);echo "</pre>";
		}else{
			return FALSE;
		}
	}*/
    
     function ext_check($obj_name) {
         $allowedExt = array("jpg","jpeg","gif","png","bmp","JPG","GIF","PNG");
         switch ($obj_name) {
	          case "image1" : $function_name = "ext_check1"; break;
	          case "image2" : $function_name = "ext_check2"; break;
	          case "image3" : $function_name = "ext_check3"; break;
	          case "image4" : $function_name = "ext_check4"; break;
	          case "image5" : $function_name = "ext_check5"; break;
	          default : $function_name = "ext_check1"; break;
         }
   
		   if (isset($_FILES) && isset($_FILES[$obj_name]) && isset($_FILES[$obj_name]["tmp_name"]) && $_FILES[$obj_name]["tmp_name"]!= "") {
		    $file_ext = next(explode(".", $_FILES[$obj_name]["name"]));
		    if (!in_array($file_ext, $allowedExt)) {
		     //$error_message='The (<font color=blue>'.$_FILES[$obj_name]["name"].'</font>) is not an image file.'; 
		     return false;
		    }
		   }
		   return true;
 	}

	function renew($classify_id){
		$this->load->model("m_classify");	
		$Z_USER = Zsession::getUserFromSession(); 
		if(isset($Z_USER["user_id"])){
			$type = serialize("classified");
			$this->m_classify->renew($classify_id);
			Zsession::setFlash("success_message", "Classified ads has been renewed successfully");
			redirect(site_url("users/profiles?type=".Zencryption::encrypt($type)));
			echo date('dd/mm/Y');
		}else {
			redirect(site_url(users));
		}	
	}
	
    function save_addnew_classify(){
    	$this->load->model("m_classify");
    	$this->load->helper('email');
    	date_default_timezone_set('Asia/Phnom_Penh');
    	$Z_USER = Zsession::getUserFromSession(); 
    	$userid=$Z_USER["user_id"];
    	$username=$Z_USER["user_login"];
        $data =array();
        $category=trim($_POST['cbocategory']);
        $productname=trim($_POST['txtproductname']);
        $desc=trim($_POST['txtdesc']);
        $price=trim($_POST['txtprice']);
        $contactname=trim($_POST['txtcontactname']);
        if($contactname==''){
        	$contactname=$username;
        }
        $address=trim($_POST['txtaddress']);
        $phone=trim($_POST['txtphone']);
        $fax=trim($_POST['txtfax']);
        $email=trim($_POST['txtemail']);
        $datepost=date("Y/m/d");
        $newdate =strtotime ( '+1 month' , strtotime ( $datepost ) ) ;
     	$dateclose = date ( 'Y/m/d' , $newdate );
        $error_message = "";
        if ($productname == "") 
        {
        	$error_message = "Title is required.";	
        }
        elseif( $price== "")
        {
    		$error_message="Price is required.";
    	}elseif (is_numeric($price)==FALSE)
    	{
    		$error_message="Price must be number only.";
    	}elseif ($this->ext_check('image1')==FALSE)
    	{
    		$error_message="The file number 1 is not image. Please upload image only.";
    	}
    	elseif ($this->ext_check('image2')==FALSE)
    	{
    		$error_message="The file number 2 is not image. Please upload image only.";
    	}
    	elseif ($this->ext_check('image3')==FALSE)
    	{
    		$error_message="The file number 3 is not image. Please upload image only.";
    	}
    	elseif ($this->ext_check('image4')==FALSE)
    	{
    		$error_message="The file number 4 is not image. Please upload image only.";
    	}
    	elseif ($this->ext_check('image5')==FALSE)
    	{
    		$error_message="The file number 5 is not image. Please upload image only.";
    	}
    	else
    	{
        	if ($email!='' && !valid_email($email)){
        		$error_message="Email is not valid.";
        	}
        }
        if ($error_message != "") {
        	$data['cbocategory']=$category;
        	$data['txtproductname']=$productname;
	        $data['txtdesc']=$desc;
	        $data['txtprice']=$price;
	        $data['txtcontactname']=$contactname;
	        $data['txtaddress']=$address;
	        $data['txtphone']=$phone;
	        $data['txtfax']=$fax;
	        $data['txtemail']=$email;
        	$data["error_message"] = $error_message;
        	$data['all_cate']=$this->m_classify->get_all_classifycategory();
    		$this->load->template("templates/general","classify_ads/addnew_classify", "Classify Ads",$data);
        	return;
        }
        //$project_path = str_replace("\\", "/", FCPATH);
    	$file_path="usr/".htmlspecialchars($Z_USER["user_id"]).'/';
       $this->m_classify->save_addnew_ads($productname,
							        	$desc,
							        	$price,
							        	$contactname,
							        	$address,
							        	$phone,
							        	$fax,
							        	$email,
							        	$datepost,
							        	$dateclose,
							        	$category,
							        	$userid,
							        	$file_path
							        	);
        
		$productid=$this->m_classify->get_product_maxid();
		$this->do_upload_pic($productid['product_id']);
		Zsession::setFlash("success_message", "Classified ads has been saved successfully");
		redirect(site_url('users/profiles?type='.Zencryption::encrypt(serialize("classified"))));
    }
    

    function do_upload_pic($productid){
    	/* Create the config for upload library */
				      /* (pretty self-explanatory) */
    	$Z_USER = Zsession::getUserFromSession(); 
				      $date=date("His");
				      $patch=FCPATH."/usr/".$Z_USER["user_id"]."/"; /* NB! create this dir! */
				      if (!file_exists ($patch)){ mkdir($patch,0707);}
				      $config["upload_path"] = $patch;
				      $config["allowed_types"] = "gif|jpg|png|bmp|jpeg|JPG|PNG|BMP|GIF";		      
				      $config["image_library"] = "gd2";
				      $config["source_image"] = $patch.htmlspecialchars($Z_USER["user_id"]);
				      $config["encrypt_name"]  = FALSE;
				      $config["overwrite"]  = TRUE;
				      $config["max_size"]  = "0";
				      $config["max_width"]  = "0";
				      $config["max_height"]  = "0";
					  $config["width"] = 480;
					  $config["height"] = 480;
				     
				      /* Load the upload library */
				      $this->load->library("upload", $config);
						
				
				      /* Create the config for image library */
				      /* (pretty self-explanatory) */
				      $configThumb = array();
				      $configThumb["image_library"] = "gd2";
				      $configThumb["thumb_marker"] = $date."_Thumb";
				      $configThumb["source_image"] = $patch.htmlspecialchars($Z_USER["user_id"]);
				      $configThumb["create_thumb"] = TRUE;
				      $configThumb["maintain_ratio"] = TRUE;
				      /* Set the height and width or thumbs */
				      /* Do not worry - CI is pretty smart in resizing */
				      /* It will create the largest thumb that can fit in those dimensions */
				      /* Thumbs will be saved in same upload dir but with a _thumb suffix */
				      /* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
				      $configThumb["width"] = 98;
				      $configThumb["height"] = 70;
				      /* Load the image library */
				      $this->load->library("image_lib");
					  $this->image_lib->initialize($config);
					  $this->image_lib->resize();
					  		      
				      $filename_arr = array();
				      $filename_big_arr = array();
				      $file_path='';
				      for($i = 1; $i <= count($_FILES); $i++) {
				        /* Handle the file upload */
				        $upload = $this->upload->do_upload("image".$i);
				        /* File failed to upload - continue */
				           
				      	if($upload === FALSE) continue;
				        	/* Get the data about the file */
				        	$data = $this->upload->data();
				        
				        	$uploadedFiles[$i] = $data;
				        	/* If the file is an image - create a thumbnail */
					        if($data["is_image"] == 1) {//if the upload file is image
					          $filename_arr["image".$i] = $data["orig_name"];
					
					          //$_FILES["image".$i]['name'];
					          $configThumb["source_image"] = $data["full_path"];     
					          
					          $this->image_lib->initialize($configThumb);
					          $this->image_lib->resize();
					           
					          //echo "<pre>"; var_dump($data); echo "</pre>";
					          $filename = $data["file_path"].$data["raw_name"].$date.$data["file_ext"];

							  //rename($data["full_path"], $filename);
							  
							  $image_obj = new Zimage();
            				  $image_obj->resize($data["full_path"], $filename);
            
					          //rename($data["full_path"], $filename);
					          $filename_big_arr["image".$i] = $data["raw_name"].$date.$data["file_ext"];
					          $img_thumb_name["image".$i] = $data["raw_name"].$date."_Thumb".$data["file_ext"];
					          //--------insert info--------------------------------------------					
							  $file_path='usr/'.htmlspecialchars($Z_USER["user_id"]).'/';
							  $this->m_classify->upload_logo($productid,$filename_big_arr["image".$i]);
							  
							  unlink($data["full_path"]);
							  $this->image_lib->clear();
            				  unset($config);
					        }else{//when upload file is not image
					          echo "<h3>The file you upload is not image.</h3>";
				  			  
					      	}//end if $data is image
				       
				    	 } // FOR LOOP
				    	 
    }
    function goto_update_classify($productid){
    	$this->load->model("m_classify");
    	$data=array();
    	$data['ads_data']=$this->m_classify->get_product_detail($productid);
    	$data['error_message']='';
    	$data['all_cate']=$this->m_classify->get_all_classifycategory();
    	$data["success_message"] = Zsession::getFlash("success_message");
    	$this->load->template("templates/general","classify_ads/update_classify", "Update Classify Ads",$data);
    }
    function save_update_classify($productid){
    	$this->load->model("m_classify");
    	$this->load->helper('email');
    	//date_default_timezone_set('Asia/Manila');
    	$Z_USER = Zsession::getUserFromSession(); 
    	$userid=$Z_USER["user_id"];
    	$username=$Z_USER["user_login"];
        $data =array();
        $category=trim($_POST['cbocategory']);
        $productname=trim($_POST['txtproductname']);
        $desc=trim($_POST['txtdesc']);
        $price=trim($_POST['txtprice']);
        $contactname=trim($_POST['txtcontactname']);
        if($contactname==''){
        	$contactname=$username;
        }
        $address=trim($_POST['txtaddress']);
        $phone=trim($_POST['txtphone']);
        $fax=trim($_POST['txtfax']);
        $email=trim($_POST['txtemail']);
        $datepost=date("Y/m/d");
        $newdate =strtotime ( '+1 month' , strtotime ( $datepost ) ) ;
     	$dateclose = date ( 'Y/m/d' , $newdate );
        $error_message = "";
        if ($productname == "") 
        {
        	$error_message = "Title is required.";	
        }
        elseif( $price== "")
        {
    		$error_message="Price is required.";
    	}elseif (is_numeric($price)==FALSE)
    	{
    		$error_message="Price must be number only.";
    	}elseif ($this->ext_check('image1')==FALSE)
    	{
    		$error_message="The file number 1 is not image. Please upload image only.";
    	}
    	elseif ($this->ext_check('image2')==FALSE)
    	{
    		$error_message="The file number 2 is not image. Please upload image only.";
    	}
    	elseif ($this->ext_check('image3')==FALSE)
    	{
    		$error_message="The file number 3 is not image. Please upload image only.";
    	}
    	elseif ($this->ext_check('image4')==FALSE)
    	{
    		$error_message="The file number 4 is not image. Please upload image only.";
    	}
    	elseif ($this->ext_check('image5')==FALSE)
    	{
    		$error_message="The file number 5 is not image. Please upload image only.";
    	}else
    	{
        	if ($email!='' && !valid_email($email)){
        		$error_message="Email is not valid.";
        	}
        }
    	if ($error_message != "") {
        	$data['cbocategory']=$category;
        	$data['txtproductname']=$productname;
	        $data['txtdesc']=$desc;
	        $data['txtprice']=$price;
	        $data['txtcontactname']=$contactname;
	        $data['txtaddress']=$address;
	        $data['txtphone']=$phone;
	        $data['txtfax']=$fax;
	        $data['txtemail']=$email;
        	$data["error_message"] = $error_message;
        	$data['ads_data']=$this->m_classify->get_product_detail($productid);
    		$data['all_cate']=$this->m_classify->get_all_classifycategory();
    		$this->load->template("templates/general","classify_ads/update_classify", "Classify Ads",$data);
        	return;
        }
        $this->m_classify->save_update_ads($productname,
							        	$desc,
							        	$price,
							        	$contactname,
							        	$address,
							        	$phone,
							        	$fax,
							        	$email,
							        	$dateclose,
							        	$category,
							        	$productid
							        	);
        
		$this->do_upload_pic($productid);
		$type = serialize("classified");
		Zsession::setFlash("success_message", "Updated classified ads has been saved successfully");
		//redirect(site_url("users/profiles?type=".Zencryption::encrypt($type)));
		redirect($_SERVER['HTTP_REFERER']);
    }
    function delete_one_img($productid,$imgid){
    	$this->load->model("m_classify");
    	$Z_USER = Zsession::getUserFromSession(); 
    	$imgname=$this->m_classify->select_img_by_id($imgid);
    	$str_path = str_replace("\\", "/", FCPATH);
    	//$project_path = str_replace("\\", "/", FCPATH);
    	$file_path=$str_path."usr/".htmlspecialchars($Z_USER["user_id"]).'/'.$imgname['img_name'];
    	
    	$file_name = current(explode(".", $imgname['img_name']));
    	$file_ext  = next(explode(".", $imgname['img_name']));
    	$file_path_thumb=$str_path."usr/".htmlspecialchars($Z_USER["user_id"]).'/'.$file_name."_Thumb.".$file_ext;
    			
    	if(file_exists($file_path)){ 
    		unlink($file_path);
    		unlink($file_path_thumb);
    	}
    			
    	$this->m_classify->delete_one_img($imgid,$productid);
    	redirect(site_url("classify/goto_update_classify/$productid"));
    }
    function delete_classify($pro_id){
    	$this->load->model("m_classify");
	    $Z_USER = Zsession::getUserFromSession();
		//$pro_id=Zencryption::decrypt($pro_id);
		$userid=$Z_USER["user_id"];
		if($this->m_classify->isMyClassified($pro_id,$userid)!= null){ 
	    	$data['all_img']=$this->m_classify->select_classify_img($pro_id);
	    	foreach($data['all_img']->result() as $imgname):
	    	$project_path = str_replace("\\", "/", FCPATH);
	    	$file_path=$project_path."usr/".htmlspecialchars($Z_USER["user_id"]).'/'.$imgname->img_name;
	    	
	    	$file_name = current(explode(".", $imgname->img_name));
    		$file_ext  = next(explode(".", $imgname->img_name));
    		$file_path_thumb=$str_path."usr/".htmlspecialchars($Z_USER["user_id"]).'/'.$file_name."_Thumb.".$file_ext;
	    	
		    if(file_exists($file_path)){ 
    		unlink($file_path);
    		unlink($file_path_thumb);
    		}
	    	$this->m_classify->delete_one_img($imgname->img_id,$pro_id);
	    	endforeach;
	    	//delete in table hits
	    	$this->m_classify->delete_product_hit($pro_id);
	    	//delete in table product_ads
	    	$this->m_classify->delete_product_ads($pro_id);
	    	$type = serialize("classified");
			redirect(site_url("users/profiles?type=".Zencryption::encrypt($type)));
		}else{
			echo "No Access Right";
		}
    }
    function renew_post($productid){
    	$this->load->model("m_classify");
    	$datepost=date("Y/m/d");
        $newdate =strtotime ( '+1 month' , strtotime ( $datepost ) ) ;
     	$dateclose = date ( 'Y/m/d' , $newdate );
     	$this->m_classify->renew_post($datepost,$dateclose,$productid);
     	$type = serialize("classified");
		redirect(site_url("users/profiles?type=".Zencryption::encrypt($type)));
    }
    function search_result(){
    	
    	$this->load->model("m_classify");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$offset = isset($_GET["per_page"]) && $_GET["per_page"]!="" ? $_GET["per_page"] : "0";
	    $config=array();
    	$product=trim($_GET['proname']);
    	$category=trim($_GET['cid']);
    	$data =array();
    	$total_ads=$this->m_classify->count_classify_search($product,$category);
    	if(count($total_ads->result()) > '0'){
    		$showmsg="";
    	}else{//when no result
    		$showmsg="Result not found. Please try again.";
    	}
	    $config['base_url'] = site_url("/classify/search_result"."?proname=".$product."&cid=".$category);
		$config['total_rows'] = count($total_ads->result());
	    $config['page_query_string'] = TRUE;
	    $config['per_page'] = '26';
	    $config['num_links'] = '9';
	    $config['full_tag_open'] = '<p>';
	    $config['full_tag_close'] = '</p>';
	    $this->pagination->initialize($config);
	    $data['result_msg']=$showmsg;
    	$data['txtsearch']=$product;
    	$data['cbocategory']=$category;
	    $data['all_cate']=$this->m_classify->get_all_classifycategory();
	    $data['search_result_per_page']=$this->m_classify->search_ads($product,$category,$config['per_page'],$offset);
	    $data['search_result']=$this->m_classify->search_count($product,$category);
	    
	        	    $data['latest_ads']=$this->m_classify->get_product_latest();
            $data['view_all_ads']=$this->m_classify->get_view_all_ads();
    	    $data['cat_car']=$this->m_classify->get_view_by_cat("1");
            $data['cat_phone']=$this->m_classify->get_view_by_cat("3");
            $data['cat_phone_acc']=$this->m_classify->get_view_by_cat("19");
            $data['cat_computer']=$this->m_classify->get_view_by_cat("2");
            $data['cat_computer_acc']=$this->m_classify->get_view_by_cat("7");
            $data['cat_books']=$this->m_classify->get_view_by_cat("15");
            $data['cat_camera']=$this->m_classify->get_view_by_cat("8");
            
            $data['cat_cloth']=$this->m_classify->get_view_by_cat("12");
            $data['cat_dvd']=$this->m_classify->get_view_by_cat("14");
            $data['cat_flower']=$this->m_classify->get_view_by_cat("13");
            $data['cat_Health']=$this->m_classify->get_view_by_cat("16");
            
            $data['cat_Home_app']=$this->m_classify->get_view_by_cat("10");
            $data['cat_Health']=$this->m_classify->get_view_by_cat("16");
            $data['cat_property']=$this->m_classify->get_view_properties();
            $data['cat_jewellery']=$this->m_classify->get_view_by_cat("9");
            
            $data['cat_motorbike']=$this->m_classify->get_view_by_cat("18");
            $data['cat_jewellery']=$this->m_classify->get_view_by_cat("9");
            $data['cat_other']=$this->m_classify->get_view_by_cat("11");
            $data['cat_job_wanted']=$this->m_classify->get_view_job_wanted();
            $data['cat_job_ann']=$this->m_classify->get_view_job_ann();
            
    	$this->load->template("templates/general","classify_ads/search_result", "Classify: Result search",$data);
    }
}
?>